﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Role;
using B.S.BasicData.Api.Read.Dtos;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.CommandHandler.Role
{
    public class QueryRoleHandler : IRequestHandler<QueryRoleCommand, ApiResult<List<RoleDto>>>
    {
        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly ILogger<QueryRoleHandler> logger;
        private readonly IMapper mapper;

        public QueryRoleHandler(IBaseRepository<RoleModel> roleRepo,ILogger<QueryRoleHandler> logger,IMapper mapper)
        {
            this.roleRepo = roleRepo;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 中介者查询所有角色的处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<RoleDto>>> Handle(QueryRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleDto>> result = new ApiResult<List<RoleDto>>();


            var list = roleRepo.Query().Result.ToList();

            result.Code = ApiEnums.Success;
            result.Msg = "查询所有角色列表成功!";
            result.Data = mapper.Map<List<RoleDto>>(list);

            return Task.FromResult(result);


        }
    }
}
